import json

import requests

from Utils.gy_kl_api.api_main import kl_api, api_url


def door_info():
    """
    广场级客流UUID查询
    """
    url = api_url + "api/v1/base/gateInfo?plaza_unid=9389eb78ba67a9160504d36f32f310ee"
    token = kl_api()
    headers = {
        "authorization": token
    }
    rep = requests.get(url, headers=headers)
    rep.content.decode()
    data = json.loads(rep.text)
    data = data['data']
    door_data = []
    for i in data:
        if i['is_mall_gate'] == 1:
            door_data.append({"gate_name": i['gate_name'], "gate_unid": i['gate_unid']})
    return door_data


def door_data(start: str, end: str):
    """
    广场级客流名称、客流量、占比任意时间段查询
    """
    if start and end != 0:
        url = api_url + "api/v1/passenger/gateDay?plaza_unid=9389eb78ba67a9160504d36f32f310ee&startDate=" + str(
            start) + "&endDate=" + str(end)
        headers = {
            "authorization": kl_api()
        }
        req = requests.get(url, headers=headers)
        req.content.decode()
        data = json.loads(req.text)
        data = data['data']
        door_insum_info = []
        data_success = []
        sum_data = []
        count = 0
        count2 = 0
        for a in data:
            count = count + 1
            sum_data.append(a['innum'])
        sum_data = sum(sum_data)
        for i in door_info():
            for e in data:
                if e['gate_unid'] in i['gate_unid']:
                    door_insum_info.append(
                        {"gate_date": e['countdate'], "gate_name": i['gate_name'], "gate_insum": e['innum']})
        data_dict = {}
        for e in door_insum_info:
            key = (e['gate_name'])
            if key in data_dict:
                data_dict[key] += int(e['gate_insum'])
            else:
                data_dict[key] = int(e['gate_insum'])
        for key, value in data_dict.items():
            count2 = count2 + 1
            data_success.append({'date_num': count2, 'gate_name': key, 'gate_insum': value,
                                 "gate_percent": str(float('%.2f' % float(value / sum_data * 100))) + "%"})
        return data_success, count
    else:
        return []


def country_kl(start: str, end: str):
    """
    全国商场客流排名sdk
    """
    info_url = "http://keliu.xincheng.com:8088/api/v1/base/plazaInfo"

    info_payload = {}
    info_headers = {
        'authorization': 'f08b0015-d2ac-4fde-8d22-e707f5bb02b0'
    }

    response = requests.request("GET", info_url, headers=info_headers, data=info_payload)
    response.content.decode()
    info_data = json.loads(response.text)
    info_data = info_data['data']
    info_list = []
    for a in info_data:
        info_list.append({"plaza_name": a['plaza_name'], "plaza_unid": a['plaza_unid']})
    # 这里开始用上面得到的项目ID去请求各个项目客流数据
    all_data_info_list = []
    for b in info_list:
        all_data_info_list = ("plaza_unid="+b['plaza_unid']+"&")
    # all_data_url = "http://keliu.xincheng.com:8088/api/v1/passenger/plazaDay?plaza_unid=" + str(
    #     b['plaza_unid']) + "&startDate=" + start + "&endDate=" + end
    # all_data_payload = {}
    # all_data_headers = {
    #     'authorization': 'f08b0015-d2ac-4fde-8d22-e707f5bb02b0'
    # }
    #
    # all_data_req = requests.request("GET", all_data_url, headers=all_data_headers, data=all_data_payload)

    print(all_data_info_list)


if __name__ == '__main__':
    country_kl("2021-05-01", "2021-05-01")
